package com.top10.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.top10.entity.Address;
import com.top10.entity.Shop;
import com.top10.entity.vo.ShopVo;
import com.top10.mapper.ShopMapper;
import com.top10.service.AddressService;
import com.top10.service.IShopService;
import com.top10.utils.DistanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ellen
 * @since 2018-09-27
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    ShopMapper shopMapper;
    @Autowired
    AddressService addressService;
    @Autowired
    IShopService shopService;

    @Override
    public List<ShopVo> shopList(String type) {
        return shopMapper.shopList(type);
    }

    @Override
    public Shop selectByUuid(String uuid){
        return shopMapper.selectByUuid(uuid);
    }


    /**
     * 获取最经代理商的shop_uuid
     * @param province
     * @param city
     * @param district
     * @param shop_uuid
     * @return
     * @throws Exception
     */
    @Override
    public String  getAgentUUid(String province, String city, String district, String shop_uuid)throws Exception{
        String query_where = DistanceUtil.fullAddress(province, city, district);
        Address address = addressService.selectByFullName(query_where);
        int temp = Integer.MAX_VALUE;
        List<Shop> shopList=shopService.selectList(new EntityWrapper<Shop>().eq("parent_uuid",shop_uuid).eq("status",1).like("sellerid", "de_%"));
        if (shopList.size() == 0) {
             shop_uuid = shopList.get(0).getUuid();
        }
        for (int i = 0; i < shopList.size(); i++) {
            String province1 = shopList.get(i).getProvince();
            String city1 = shopList.get(i).getCity();
            String district1 = shopList.get(i).getDistrict();
            String fullAddress = DistanceUtil.fullAddress(province1, city1, district1);

            Address agentAddress = addressService.selectByFullName(fullAddress);

            String agentLat = agentAddress.getLat();
            String agentIng = agentAddress.getIng();

            String distanceStr = DistanceUtil.getDistance(address.getLat(), address.getIng(), agentLat, agentIng);
            int distance = Integer.parseInt(distanceStr);
            // 保存距离最小的uuid
            if (distance < temp) {
                temp = distance;
                shop_uuid = shopList.get(i).getUuid();
              /*      System.out.println("当前距离最小代理商地址：" + fullAddress + "当前距离：" + temp);
                    System.out.println("当前代理商uuid：" + uuid);
                    System.out.println("当前的代理商店铺名称：" + agentShopList.get(i).getName());*/
            }
        }
        return shop_uuid;
    }
    /**
     * 获取最经代理商的shop_uuid,用于查找代理商的
     * @param province
     * @param city
     * @param district
     * @param shop_uuid
     * @return
     * @throws Exception
     */
    @Override
    public String  getAgentUUidone(String province, String city, String district, String shop_uuid)throws Exception{
        String query_where = DistanceUtil.fullAddress(province, city, district);
        Address address = addressService.selectByFullName(query_where);
        int temp = Integer.MAX_VALUE;
        List<Shop> shopList=shopService.selectList(new EntityWrapper<Shop>().eq("parent_uuid",shop_uuid).eq("status",1).eq("shop_type",5));
        if (shopList.size() == 0) {
            shop_uuid = shopList.get(0).getUuid();
        }
        for (int i = 0; i < shopList.size(); i++) {
            String province1 = shopList.get(i).getProvince();
            String city1 = shopList.get(i).getCity();
            String district1 = shopList.get(i).getDistrict();
            String fullAddress = DistanceUtil.fullAddress(province1, city1, district1);

            Address agentAddress = addressService.selectByFullName(fullAddress);

            String agentLat = agentAddress.getLat();
            String agentIng = agentAddress.getIng();

            String distanceStr = DistanceUtil.getDistance(address.getLat(), address.getIng(), agentLat, agentIng);
            int distance = Integer.parseInt(distanceStr);
            // 保存距离最小的uuid
            if (distance < temp) {
                temp = distance;
                shop_uuid = shopList.get(i).getUuid();
              /*      System.out.println("当前距离最小代理商地址：" + fullAddress + "当前距离：" + temp);
                    System.out.println("当前代理商uuid：" + uuid);
                    System.out.println("当前的代理商店铺名称：" + agentShopList.get(i).getName());*/
            }
        }
        return shop_uuid;
    }
}
